﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace AutoSend
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
            
            //Myinfo.skinname = System.Configuration.ConfigurationManager.AppSettings["skinname"].ToString();
            //if (Myinfo.skinname != "no")
            //{
            //    Myinfo.skin.SkinFile = Myinfo.skinname;
            //    Myinfo.skin.Active = true;
            //    Myinfo.skin.SkinAllForm = true;
            //}
            //else
            //{
            //    Myinfo.skin.Active = false;
            //}
            string mFilePath = Application.StartupPath + "\\AutoUpdate.exe";
            if(File.Exists(mFilePath))
            Process.Start(mFilePath);
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            
            this.Text = Myinfo.sname + "-" + Myinfo.comname;
            this.tsslQQ.Text = "客服QQ：" + Myinfo.QQ;
            this.tsslPhone.Text = "咨询电话：" + Myinfo.phone;
            List<string> source = AShelp.Load("user");
            if (source != null)
            {
                this.txtName.Text = source[0];
                this.txtPwd.Text = source[1];
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string pass = this.txtPwd.Text;
            LoginServer(name, pass);
            
        }

        private void LoginServer(string name, string pass)
        {
            string key = "AutoSend";
            string iv = "weiwei@1";

            //strPass格式：name|pass|blongSys_id|当前时间
            //blongSys_id要和数据库表belongSys中的blongSys_id对应
            string strPass = name + "|" + pass + "|"+Myinfo.sid+"|" + DateTime.Now;

            string md5strPass = Tools.Encode(strPass, key, iv);

            //HeartWebService.HeartSoapClient hwm = new HeartWebService.HeartSoapClient();
            WebReference2.Heart hwm = new WebReference2.Heart();
            try
            {

                string re = "-1";
                int isProbation = 0;
                string nowtime = "";

                string result = hwm.Login(md5strPass);
                DateTime dtreg, dtnow;
                dtreg = DateTime.Now;
                dtnow = DateTime.Now;

                if (!string.IsNullOrEmpty(result))
                {
                    //格式为：
                    //1.    result|服务器时间，
                    //      result：-1、用户名密码错误，-2、此用户在其他地方登录，-3、用户无授权
                    //
                    //2.    result|isProbation|服务器时间，
                    //      result：到期时间
                    //      isProbation：是否试用，0、否，1、是
                    result = Tools.Decode(result, key, iv);

                    string[] r = result.Split('|');

                    if (r.Length == 2)
                    {
                        re = r[0];
                    }
                    if (r.Length == 3)
                    {
                        re = r[0];
                        isProbation = Convert.ToInt32(r[1]);
                        nowtime = r[2];
                        DateTime.TryParse(re, out dtreg);
                        DateTime.TryParse(nowtime, out dtnow);
                    }


                }

                if (re == "-1")//
                {
                    MessageBox.Show("用户名密码错误！");
                    return;
                }
                if (re == "-2")
                {
                    MessageBox.Show("此用户已登录,无法重复登陆!");
                    return;
                }
                if (re == "-3")
                {
                    MessageBox.Show("用户无授权！");
                    return;
                }

                if (isProbation == 1)
                {
                    if (dtreg <= dtnow)
                    {
                        MessageBox.Show("试用期已到期！");
                        
                    }
                    else
                    {
                        //MessageBox.Show("欢迎试用本软件！");
                        //Myinfo.days = re;
                        DateTime n=DateTime.Now;
                        DateTime.TryParse(nowtime,out n);
                        DateTime s=DateTime.Now;
                        DateTime.TryParse(re,out s);
                        Myinfo.softtime = s - n;
                        if (ckbremenber.Checked)
                        {
                            AShelp.Save(this.txtName.Text, this.txtPwd.Text, "", "", "user");
                            Myinfo.username = this.txtName.Text;
                        }
                        frmMain f = new frmMain(name, pass);
                        f.Show();
                        Myinfo.myfmain = f;
                        this.Hide();
                    }



                }
                else
                {
                    if (dtreg <= dtnow)
                    {
                        MessageBox.Show("授权已到期！");
                    }
                    else
                    {
                        //Myinfo.days = re;
                        DateTime n = DateTime.Now;
                        DateTime.TryParse(nowtime, out n);
                        DateTime s = DateTime.Now;
                        DateTime.TryParse(re, out s);
                        Myinfo.softtime = s - n;
                        if (ckbremenber.Checked)
                        {
                            AShelp.Save(this.txtName.Text, this.txtPwd.Text, "", "", "user");
                            Myinfo.username = this.txtName.Text;
                        }
                        frmMain f = new frmMain(name, pass);
                        f.Show();
                        Myinfo.myfmain = f;
                        this.Hide();
                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器未连接成功！");
            }
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                this.btnOK.PerformClick();
            }


        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (Myinfo.myfmain != null)
            {
                Myinfo.myfmain.Show();
                Myinfo.myfmain.WindowState = FormWindowState.Normal;
                Myinfo.myfmain.ShowInTaskbar = true;
                Myinfo.myfmain.Activate();
            }

        }

        private void 打开软件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Myinfo.myfmain != null)
            {
                Myinfo.myfmain.Show();
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Myinfo.myfmain != null)
            {
                Myinfo.myfmain.Close();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (Myinfo.myfmain != null)
            {
                Myinfo.myfmain.Show();
            }
        }
    }
}
